package com.google.fbyoutube.client.widget;

import com.google.fbyoutube.client.YouTube;
import com.google.fbyoutube.shared.FBUtility;
import com.google.fbyoutube.shared.ytVideoEntry;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.IFrameElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.VerticalPanel;

import pl.rmalinowski.gwt2swf.client.ui.SWFWidget;

public class WidVideoBox extends Composite {

  private ytVideoEntry vod;

  /**
   * @param video
   */
  public WidVideoBox(ytVideoEntry video) {

    this.vod = video;

    VerticalPanel verticalPanel = new VerticalPanel();
    verticalPanel.setSpacing(5);
    verticalPanel.setStyleName("gwt-PushButton");
    initWidget(verticalPanel);

    InlineLabel nlnlblThisIsThe = new InlineLabel(this.vod.getTitle());
    nlnlblThisIsThe.setStyleName("userComLayout");
    verticalPanel.add(nlnlblThisIsThe);

    final String videoUrl =
        "https://www.youtube.com/embed/" + this.vod.getId();
//    SWFWidget widget = new SWFWidget(videoUrl, 500, 340);
    Frame widget = new Frame(videoUrl);
    widget.setWidth("570px");
    widget.setHeight("340px");
    widget.getElement().setAttribute("frameborder", "0");
    widget.getElement().setAttribute("allowfullscreen", "true");
    widget.getElement().setId("ytplayer");

    verticalPanel.add(widget);
//    widget.setSize("570px", "340px");
//    widget.addParam("allowFullScreen", "true");
//    widget.addParam("allowScriptAccess", "always");
//    widget.addParam("wmode", "opaque");
//    widget.addParam("movie", videoUrl);

    HorizontalPanel horizontalPanel = new HorizontalPanel();
    horizontalPanel.setStyleName("panel-round");
    horizontalPanel.setSpacing(5);
    verticalPanel.add(horizontalPanel);
    horizontalPanel.setWidth("0%");	// Fit the buttons

    Button button = new Button("Favorite");
//    horizontalPanel.add(button);
    button.setSize("80px", "35px");

    Button button_1 = new Button("Post to wall");
    button_1.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        
        String pic = null;
        if (vod.getMediaGroupThmbnails() != null && vod.getMediaGroupThmbnails().size() > 0) {
          pic = vod.getMediaGroupThmbnails().get(0);
        }
        
        JSONObject data = new JSONObject ();
        data.put( "method", new JSONString ( "feed" ) );
        data.put( "link", new JSONString ( buildVideoUrl()) );
        data.put( "name", new JSONString ( vod.getTitle() ) );
        data.put( "picture", new JSONString ( pic ) );
        data.put( "caption", new JSONString ( "Dear, I recommand you to watch this funny video!" ) );
        data.put( "description", new JSONString ( vod.getSummaryPlainText() ) );
//        data.put( "source", new JSONString ( videoUrl ) );

        
        data.put( "user_message_prompt", new JSONString ( "Share your thoughts about " + FBUtility.APP_NAME ) );
        
        YouTube.getAppGUI().fbCore.ui(data.getJavaScriptObject(), new AsyncCallback<JavaScriptObject>() {

          @Override
          public void onFailure(Throwable caught) {
            GWT.log("Message send failed");
          }

          @Override
          public void onSuccess(JavaScriptObject result) {
            GWT.log("Message send success " + new JSONObject(result).toString());
          }});
      }
    });
    horizontalPanel.add(button_1);
    button_1.setSize("90px", "35px");

    Button button_2 = new Button("Invite friends");
    button_2.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        JSONObject data = new JSONObject ();
        data.put( "method", new JSONString ( "apprequests" ) );
        data.put( "message", new JSONString ( "Hi, I invite you to join Youtube on Facebook!" ) );
        
        YouTube.getAppGUI().fbCore.ui(data.getJavaScriptObject(), new AsyncCallback<JavaScriptObject>() {
          
          @Override
          public void onSuccess(JavaScriptObject result) {
            // TODO(xiaodao): Auto-generated method stub
            
          }
          
          @Override
          public void onFailure(Throwable caught) {
            // TODO(xiaodao): Auto-generated method stub
            
          }
        });
      }
    });
    
    horizontalPanel.add(button_2);
    button_2.setSize("100px", "35px");

    Button button_3 = new Button("Send to friend");
    button_3.addClickHandler(new ClickHandler() {
		
		@Override
		public void onClick(ClickEvent event) {
	        JSONObject data = new JSONObject ();
	        data.put( "method", new JSONString ( "send" ) );
	        data.put( "name", new JSONString ( "Hi, I shared a video with you, " + vod.getTitle() ) );
	        data.put( "link", new JSONString ( buildVideoUrl() ) );
	        
	        YouTube.getAppGUI().fbCore.ui(data.getJavaScriptObject(), new AsyncCallback<JavaScriptObject>() {
	          
	          @Override
	          public void onSuccess(JavaScriptObject result) {
	            // TODO(xiaodao): Auto-generated method stub
	            
	          }
	          
	          @Override
	          public void onFailure(Throwable caught) {
	            // TODO(xiaodao): Auto-generated method stub
	            
	          }
	        });
	      }
	});
    horizontalPanel.add(button_3);
    button_3.setSize("100px", "35px");
    
    Button button_4 = new Button("Add to Page");
    button_4.addClickHandler(new ClickHandler() {
		
		@Override
		public void onClick(ClickEvent event) {
	        JSONObject data = new JSONObject ();
	        data.put( "method", new JSONString ( "pagetab" ) );
	        data.put( "redirect_uri", new JSONString ( buildVideoUrl() ) );
	        
	        YouTube.getAppGUI().fbCore.ui(data.getJavaScriptObject(), new AsyncCallback<JavaScriptObject>() {
	          
	          @Override
	          public void onSuccess(JavaScriptObject result) {
	            // TODO(xiaodao): Auto-generated method stub
	            
	          }
	          
	          @Override
	          public void onFailure(Throwable caught) {
	            // TODO(xiaodao): Auto-generated method stub
	            
	          }
	        });
	      }
	});
    horizontalPanel.add(button_4);
    button_4.setSize("110px", "35px");

    DisclosurePanel disclosurePanel = new DisclosurePanel("Description", true);
    disclosurePanel.setStyleName("panel-round");
    disclosurePanel.setAnimationEnabled(true);
    verticalPanel.add(disclosurePanel);
    verticalPanel.setCellHorizontalAlignment(disclosurePanel, HasHorizontalAlignment.ALIGN_CENTER);
    disclosurePanel.setWidth("100%");

    VerticalPanel verticalPanel_1 = new VerticalPanel();
    disclosurePanel.setContent(verticalPanel_1);
    verticalPanel_1.setSize("100%", "100%");
    
    InlineLabel nlnlblNewInlinelabel = new InlineLabel(video.getMediaGroupDescription());
    verticalPanel_1.add(nlnlblNewInlinelabel);

  }

private String buildVideoUrl() {
	return FBUtility.APP_URL + '?' + FBUtility.VIDEO_ID_PARAM + "=" + vod.getId();
}

}
